<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:include="include/admin/adminHeader::html('子分类管理')">
</head>
<!--<script src="webjars/jquery/2.0.0/jquery.min.js"></script>-->
<!--<link rel="stylesheet" href="webjars/bootstrap/3.0.1/css/bootstrap.min.css">-->
<!--<script src="webjars/bootstrap/3.0.1/js/bootstrap.min.js"></script>-->


<body>
<div id="workingArea" class="container-fluid">
    <!--导航栏-->
    <div th:replace="include/admin/adminNavigator::html"></div>

    <!--左侧折叠菜单-->
    <div th:include="include/admin/adminLeftMenu::html"></div>

    <div class="col-md-10">
        <ol class="breadcrumb">
            <li class="active">班级管理</li>
        </ol>
        <!-- Button trigger modal -->

<!--查询模块-->
        <div class="form-inline">
            <div class="form-group">
                <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal1">
                    新增班级
                </button>
            </div>
            <div class="form-group">
                <label class="col-md-6 control-label">查询类型：</label>
                <div class="col-md-6">
                    <select class="form-control" v-model="keyword_type">
                        <option value="name">班级名称</option>
                        <option value="type">班级类型</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="keyword" class="control-label col-md-4">关键字：</label>
                <div class="col-md-8">
                    <input id="keyword" class="form-control" v-model="keyword">
                </div>
            </div>
            <div class="form-group">
                <button @click="search">查找</button>
                <button @click="all">显示全部</button>
            </div>
        </div>

        <table class="table table-bordered table-responsive ">
            <thead>
            <tr>
                <th>ID</th>
                <th>班级名称</th>
                <th>班级地址</th>
                <th>班级容量</th>
                <th>班级人数</th>
                <th>班级类型</th>
                <th>活动上传</th>
                <th>班级学生</th>
                <th>删除</th>
                <th>编辑</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="bean in beans">
                <td>{{bean.id}}</td>
                <td>{{bean.name}}</td>
                <td>{{bean.address}}</td>
                <td>{{bean.studentmax}}</td>
                <td>{{bean.studentnum}}</td>
                <td>{{bean.type.name}}</td>
                <td><a :href="'activityList?cid='+bean.id"><span class="glyphicon glyphicon-list"></span></a></td>
                <td><a :href="'classStudent?cid='+bean.id"><span class="glyphicon glyphicon-list"></span></a></td>
                <td><a href="#" @click="deleteBean(bean.id)"><span class="glyphicon glyphicon-trash"></span></a></td>
                <td><a href="#" @click="getBean(bean.id)" data-toggle="modal" data-target="#myModal"><span
                        class="glyphicon glyphicon-edit"></span></a></td>
            </tr>
            </tbody>
        </table>
        <div th:replace="include/admin/adminPage::html"></div>
    </div>

    <div th:replace="include/admin/adminFoot::html"></div>

    <!-- 增加Modal -->
    <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel1">增加一个班级</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group">
                            <label class="col-md-4 control-label">班级名称：</label>
                            <div class="col-md-6">
                                <input name="name" class="form-control" v-model="bean.name"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-4 control-label">班级地址：</label>
                            <div class="col-md-6">
                                <input type="text" name="address" class="form-control" v-model="bean.address">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-4 control-label">班级最大容量：</label>
                            <div class="col-md-6">
                                <input type="text" name="studentmax" class="form-control" v-model="bean.studentmax">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-4 control-label">班级类型：</label>
                            <div class="col-md-6">
                                <select name="type" class="form-control" v-model="bean.type">
                                    <option v-for="type in types" :value="type">{{type.name}}</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button id="add" type="button" class="btn btn-primary" @click="add" >Post</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 修改Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">修改子分类信息</h4>
                </div>
                <div class="modal-body">
                    <form id="form_upload" class="form-horizontal">
                        <div class="form-group">
                            <label class="col-md-4 control-label">班级名称：</label>
                            <div class="col-md-6">
                                <input name="name" class="form-control" v-model="bean_get.name"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-4 control-label">班级地址：</label>
                            <div class="col-md-6">
                                <input type="text" name="address" class="form-control" v-model="bean_get.address">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-4 control-label">班级容量：</label>
                            <div class="col-md-6">
                                <input type="text" name="studentmax" class="form-control" v-model="bean_get.studentmax">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-4 control-label">班级类型：</label>
                            <div class="col-md-6">
                                <select name="type" class="form-control" v-model="bean_get.type">
                                    <option v-for="type in types" :value="type">{{type.name}}</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button id="upload" type="button" class="btn btn-primary" @click="update" >Modify</button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    $(function () {
        var data4Vue = {
            uri: "class",
            pagination: {},
            beans: [],
            bean: {id: 0, name: "", address: "", studentmax: 0, studentnum: 0, type: {id: 0}},
            bean_get: {id: 0, name: "", address: "", studentmax: 0, studentnum: 0, type: {id: 0}},
            types: [],
            /*关键字查找类型*/
            keyword_type:"",
            /*关键字*/
            keyword:"",
            isLook:false
        };

        var vue = new Vue({
            el: "#workingArea",
            data: data4Vue,
            mounted: function () {
                this.getType();
                this.list(0);
            },
            methods: {
                getType: function () {
                    var url = "subclass";
                    axios.get(url).then(function (response) {
                        vue.types = response.data.data;
                    })
                },
                list: function (start) {
                    var url ;
                    url= this.uri + "?start=" + start;
                    if(this.isLook)
                         url="lookup?type="+this.keyword_type+"&keyword="+this.keyword+"&start=" + start;
                    axios.get(url).then(function (response) {
                        vue.beans = response.data.data.content;
                        vue.pagination = response.data.data;
                        console.log(vue.beans);
                    })
                },
                /*查询功能*/
                search:function () {
                    this.isLook=true;
//                    alert("type:"+this.keyword_type+"---keyword:"+this.keyword);
                    var url="lookup?type="+this.keyword_type+"&keyword="+this.keyword;
//                    var url="lookup";
                    /*                    设置当前为模糊查询状态*/
                    axios.get(url).then(function (response) {
                        vue.beans = response.data.data.content;
                        vue.pagination = response.data.data;
                        console.log(vue.beans);
                    })
                },
                all:function () {
                    this.isLook=false;
                    vue.list(0);
                },
                add: function () {
                    /*bootstrap提交验证*/
                    var validator= $('form').data('bootstrapValidator').validate();
                    if(!validator.isValid()){
                        //没有通过校验
                        return;
                    } else {
                        //通过校验，可进行提交等操作
                        $('#myModal1').modal("hide");
                    }
                    axios.post(this.uri, this.bean).then(function (response) {
                        if (response.data.code == 0) {
//                            alert("增加成功");
                            vue.bean = {id: 0, name: "", description: "", category: {id: 0}};
                            vue.list(0);
                        }
                    })
                },
                getBean: function (id) {
                    var url = this.uri + "/" + id;
                    axios.get(url).then(function (response) {
                        vue.bean_get = response.data.data;
                    })

                },
                deleteBean: function (id) {
                    if (!checkDeleteLink())
                        return;
                    var url = this.uri + "/" + id;
                    axios.delete(url).then(function (response) {
                        if (response.data.code == 0) {
                            vue.list(0);
                        }
                    })
                },
                update: function () {
                    /*bootstrap提交验证*/
                    var validator= $('#form_upload').data('bootstrapValidator').validate();
                    if(!validator.isValid()){
                        //没有通过校验
                        return;
                    } else {
                        //通过校验，可进行提交等操作
                        $('#myModal').modal("hide");
                    }
                    axios.put(this.uri, this.bean_get).then(function (response) {
                        if (response.data.code == 0) {
                            vue.list(0);
                        } else
                            alert("修改失败");
                    })
                },
                jump: function (page) {
                    jump(page, vue); //定义在adminHeader.html 中
                },
                jumpByNumber: function (start) {
                    jumpByNumber(start, vue);
                }
            }
        })
    })
</script>

<!--表单校验-->
<script>
    $(function () {
        $('form').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
//                valid: 'glyphicon glyphicon-ok',
//                invalid: 'glyphicon glyphicon-remove',
//                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                /*教学分类*/
                name:{
                    validators:{
                        notEmpty:{
                            message:'班级名称不能为空'
                        }
                    }
                },
//                地址
                address:{
                    validators:{
                        notEmpty:{
                            message:'班级地址不能为空'
                        }
                    }
                },
//                最大学生数
                studentmax:{
                    validators:{
                        notEmpty:{
                            message:'班级容量不能为空'
                        },
                        regexp: {
                            /*年龄1-120*/
                            regexp: /^\d{2}$/,
                            message: "请输入两位数的合法数字"
                        }

                    }
                },
//                班级类型
                type: {
                    message: '班级验证失败',
                    validators: {
                        notEmpty: {
                            message: '请选择一个班级类型'
                        },
                        callback: {
//                            message: '必须选择一个文章类型',
                            callback: function (value, validator) {
                                if (value == -1) {
                                    return false;
                                } else {
                                    return true;
                                }

                            }
                        }
                    }
                }
            }
        });
    });
</script>
</html>